package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.constant.ClueStatusConstants;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerAddDTO;
import com.qk.dto.customer.CustomerListDTO;
import com.qk.dto.customer.CustomerUpdateDTO;
import com.qk.entity.Clue;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;

import java.time.LocalDateTime;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户Service实现类
 */
@Slf4j
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    
    @Autowired
    private CustomerMapper customerMapper;
    
    /**
     * 客户列表查询
     * @param dto 查询条件
     * @return 分页结果
     */
    @Override
    public PageResult<CustomerDO> getCustomerList(CustomerListDTO dto) {
        // 计算偏移量
        Integer offset = (dto.getPage() - 1) * dto.getPageSize();
        
        // 查询列表
        List<CustomerDO> customerList = customerMapper.selectCustomerList(dto, offset, dto.getPageSize());
        
        // 查询总数
        Integer total = customerMapper.selectCustomerCount(dto);
        
        // 构造返回结果
        return PageResult.<CustomerDO>builder()
                .total(total)
                .rows(customerList)
                .build();
    }
    
    /**
     * 根据ID查询客户
     * @param id 客户ID
     * @return 客户信息
     */
    @Override
    public Customer getById(Integer id) {
        return customerMapper.selectById(id);
    }
    
    /**
     * 添加客户
     * @param dto 客户信息
     */
    @Override
    public void addCustomer(CustomerAddDTO dto) {
        // 参数校验
        if (ObjectUtil.isEmpty(dto.getPhone()) || ObjectUtil.isEmpty(dto.getName())) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }

        // 将DTO 转成实体 bean 拷贝
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);

        // 补全创建时间
        customer.setCreateTime(LocalDateTime.now());

        // 补全更新时间
        customer.setUpdateTime(LocalDateTime.now());

        // 保存数据到
        customerMapper.insert(customer);
    }
    
    @Override
    public void updateCustomer(CustomerUpdateDTO dto) {
        // 参数校验
        if (ObjectUtil.isEmpty(dto.getId())) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        
        // 校验客户是否存在
        Customer customer = customerMapper.selectById(dto.getId());
        if (ObjectUtil.isEmpty(customer)) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        
        // 将DTO转成实体进行拷贝，只更新非空字段
        BeanUtil.copyProperties(dto, customer, true);
        
        // 更新时间
        customer.setUpdateTime(LocalDateTime.now());
        
        // 保存更新
        customerMapper.updateById(customer);
    }
}